//
//  GGWelcomeViewController.swift
//  SwiftWeico
//
//  Created by denjunjie1 on 15/12/17.
//  Copyright © 2015年 Gel. All rights reserved.
//

import UIKit

class GGWelcomeViewController: UIViewController {
    
    private var iconViewBottomCon : NSLayoutConstraint?
    

    override func viewDidLoad() {
        super.viewDidLoad()

        // Do any additional setup after loading the view.
        
        prepareUI()
    }

    
    override func viewDidAppear(animated: Bool) {
        super.viewDidAppear(animated)
        
        iconViewBottomCon?.constant = -(UIScreen.mainScreen().bounds.height - 160)
        
        UIView.animateWithDuration(1.5, delay: 0, usingSpringWithDamping: 0.5, initialSpringVelocity: 3, options: UIViewAnimationOptions(rawValue: 0), animations: { () -> Void in
            
            self.view.layoutIfNeeded()
            
            }) { (_) -> Void in
                
                self.welcomeLabel.alpha = 0
                
                UIView.animateWithDuration(1, animations: { () -> Void in
                    
                    self.welcomeLabel.hidden = false
                    self.welcomeLabel.alpha = 1
                })
                
        }
        
    }
    
    func prepareUI(){
    
        view.addSubview(bkgImage)
        view.addSubview(iconImage)
        view.addSubview(welcomeLabel)
        
        bkgImage.translatesAutoresizingMaskIntoConstraints = false
        iconImage.translatesAutoresizingMaskIntoConstraints = false
        welcomeLabel.translatesAutoresizingMaskIntoConstraints = false
        
        view.addConstraint(NSLayoutConstraint(item: bkgImage, attribute: NSLayoutAttribute.Top, relatedBy: NSLayoutRelation.Equal, toItem: view, attribute: NSLayoutAttribute.Top, multiplier: 1, constant: 0))
        view.addConstraint(NSLayoutConstraint(item: bkgImage, attribute: NSLayoutAttribute.Left, relatedBy: NSLayoutRelation.Equal, toItem: view, attribute: NSLayoutAttribute.Left, multiplier: 1, constant: 0))
        view.addConstraint(NSLayoutConstraint(item: bkgImage, attribute: NSLayoutAttribute.Right, relatedBy: NSLayoutRelation.Equal, toItem: view, attribute: NSLayoutAttribute.Right, multiplier: 1, constant: 0))
        view.addConstraint(NSLayoutConstraint(item: bkgImage, attribute: NSLayoutAttribute.Bottom, relatedBy: NSLayoutRelation.Equal, toItem: view, attribute: NSLayoutAttribute.Bottom, multiplier: 1, constant: 0))
        
        
        view.addConstraint(NSLayoutConstraint(item: iconImage, attribute: NSLayoutAttribute.CenterX, relatedBy: NSLayoutRelation.Equal, toItem: bkgImage, attribute: NSLayoutAttribute.CenterX, multiplier: 1, constant: 0))
        
       iconViewBottomCon =  NSLayoutConstraint(item: iconImage, attribute: NSLayoutAttribute.Bottom, relatedBy: NSLayoutRelation.Equal, toItem: bkgImage, attribute: NSLayoutAttribute.Bottom, multiplier: 1, constant: -160)
        view.addConstraint(iconViewBottomCon!)
        
        
        view.addConstraint(NSLayoutConstraint(item: welcomeLabel, attribute: NSLayoutAttribute.CenterX, relatedBy: NSLayoutRelation.Equal, toItem: iconImage, attribute: NSLayoutAttribute.CenterX, multiplier: 1, constant: 0))
        view.addConstraint(NSLayoutConstraint(item: welcomeLabel, attribute: NSLayoutAttribute.Top, relatedBy: NSLayoutRelation.Equal, toItem: iconImage, attribute: NSLayoutAttribute.Bottom, multiplier: 1, constant: 10))
        
        
    }

    private lazy var bkgImage : UIImageView = UIImageView(image: UIImage(named:"ad_background"))
    
    private lazy var iconImage : UIImageView = {
        
        let iconImg = UIImageView()
        
        iconImg.image = UIImage(named: "avatar_default_big")
        
        iconImg.layer.cornerRadius = 42.5
        iconImg.layer.masksToBounds = true
        
        return iconImg
    }()
    
    private lazy var welcomeLabel : UILabel = {
        
        let label = UILabel()
        
        label.text = "Welcome Back"
        
        label.font = UIFont.systemFontOfSize(14)
        
        label.textColor = UIColor.darkGrayColor()
        
        label.hidden = true
        
        return label
    }()
    

}
